// var express = require('express');
// var app = express();
// var expressWs = require('express-ws')(app);
// app.ws('/ws', function (ws, req) {
//     ws.on('connection', function(conn) {
//         console.log('-------------');
//         setInterval(function timeout() {
//             console.log('conn');
//         }, 500);
//
//     });
//
//     ws.on('close', function() {
//         console.log('The connection was closed!');
//     });
//
//     ws.on('error', function() {
//         console.log('websocket error!');
//     });
//
//     ws.on('message', function(msg) {
//             ws.send("500 delay");
//     });
// })
// var aWss = expressWs.getWss('/ws');
// setInterval(function () {
//     aWss.clients.forEach(function (client) {
//         client.send('hello');
//     });
// }, 5000);
//
// app.listen(4000);
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var client = require('redis').createClient();
let data = require('../libs/data');
let activity = require('../service/activity');
io.on('connection', function (socket) {
    // 监听客户端的登陆
    socket.on('login', function (obj) {

        // // 用户id设为socketid
        // socket.id = obj.uid;
        //
        // // 如果没有这个用户，那么在线人数+1，将其添加进在线用户
        // if (!onlineUsers.hasOwnProperty(obj.uid)) {
        //     onlineUsers[obj.uid] = obj.username;
        //     onlineCount++;
        // }
        //
        // // 向客户端发送登陆事件，同时发送在线用户、在线人数以及登陆用户
        client.get('activityid_start_time_' + obj.activityid, function (err, object) {
            if (!err && object != null) {
                io.emit('login', {userid: obj.userid, time: object, nowtime: Date.parse(new Date())});
            }
        });
        console.log(obj.userid + '加入了活动：' + obj.activityid);
    })

    // 监听客户端的断开连接
    socket.on('disconnect', function () {
        console.log('有人F退出了活动');
    })

    // 监听客户端发送的信息
    socket.on('message', function (obj) {
        console.log(obj);
        io.emit('message', obj);
        console.log(obj.username + "说:" + obj.content);
    })

})

server.listen(7777, function (err) {
    console.log('Listening at *:3300');
})
module.exports = {
    // 开始答题
    start (req, res) {
        console.log('开始活动', req.body);
        var time = (new Date()).valueOf();
        client.set('activityid_start_time_' + req.body.activityid, time, function (err) {
            console.log(err)
        });
        client.expire('activityid_start_time_' + req.body.activityid, 3600);
        io.emit('message', {
            activityid: req.body.activityid,
            tag: 'start',
            time: time,
            nowtime: Date.parse(new Date())
        });
        data.code = '200';
        data.msg = '开始答题成功';
        data.data = '';
        res.status(200).send(JSON.stringify(data));
    },
    // 结束答题
    end (req, res) {
        console.log('结束活动');
        client.del('activityid_start_time_' + req.body.activityid, function (err) {
            console.log(err);
        });
        if (activity.updateActive(req.body.activityid)) {
            io.emit('message', {
                activityid: req.body.activityid,
                tag: 'end'
            });
            data.code = '200';
            data.msg = '结束活动成功';
            data.data = '';
            res.status(200).send(JSON.stringify(data));
        } else {
            data.code = '104';
            data.msg = '结束活动失败';
            data.data = '';
            res.status(200).send(JSON.stringify(data));
        }
    }
};